package com.weme.chamet.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class CompassView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val indicatorPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var azimuthDegrees: Float = 0f

     var currentRotationAngle: Float = 0f
    private val tickMarkCount = 12 // 刻度线数量
    private val tickMarkLength = 20f // 刻度线长度
    private val tickMarkColor = Color.BLACK // 刻度线颜色
    private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = Color.BLACK
        textSize = 30f
        textAlign = Paint.Align.CENTER
    }
    private val tickMarkPaint = Paint().apply {
        color = tickMarkColor
        strokeWidth = 5f // 刻度线的宽度
    }
    private val indicatorLineColor = Color.RED
    private val indicatorLinePaint = Paint().apply {
        color = indicatorLineColor
        strokeWidth = 5f
    }
    init {
        circlePaint.color = Color.parseColor("#80C4C4C4") // Light Gray
        indicatorPaint.color = Color.parseColor("#FF4081") // Pink
        indicatorPaint.strokeWidth = 8f
    }

    fun updateAzimuth(azimuthDegrees: Float) {
        this.azimuthDegrees = azimuthDegrees
        invalidate()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val centerX = width / 2f
        val centerY = height / 2f
        val radius = Math.min(centerX, centerY) - 20f

        // Draw the circle
        canvas?.drawCircle(centerX, centerY, radius, circlePaint)

//        // Draw the tick marks and values
//        val degreePerTick = 360f / tickMarkCount
//        for (i in 0 until tickMarkCount) {
//            val angle = i * degreePerTick
//            val startX = centerX + radius * cos(Math.toRadians(angle.toDouble())).toFloat()
//            val startY = centerY - radius * sin(Math.toRadians(angle.toDouble())).toFloat()
//            val endX = startX + tickMarkLength * cos(Math.toRadians(angle.toDouble())).toFloat()
//            val endY = startY - tickMarkLength * sin(Math.toRadians(angle.toDouble())).toFloat()
//
//            canvas?.drawLine(startX, startY, endX, endY, tickMarkPaint)
//
//            // Draw tick mark values (0, 30, 60, ...)
//            if (i % 3 == 0) {
//                canvas?.drawText("$i", endX, endY - 10, textPaint)
//            }
//        }
//
//        // Calculate the rotation angle
//        val rotationAngle = -azimuthDegrees
//
//        // Save the canvas state
//        canvas?.save()
//
//        // Rotate the canvas to draw the indicator line
//        canvas?.rotate(rotationAngle, centerX, centerY)
//
//        // Draw the indicator line
//        canvas?.drawLine(centerX, centerY, centerX, centerY - radius, indicatorLinePaint)
//
//
//        // Calculate the position of the indicator line's start and end points
//        val indicatorStartX = centerX
//        val indicatorStartY = centerY - radius
//        val indicatorEndX = centerX
//        val indicatorEndY = centerY - radius * 0.8f
//
//        // Draw the indicator line using the drawable image
//        val drawable = context.getDrawable(R.drawable.pic_sd1)
//        drawable?.setBounds(
//            (indicatorStartX - drawable.intrinsicWidth / 2).toInt(),
//            indicatorStartY.toInt(),
//            (indicatorEndX + drawable.intrinsicWidth / 2).toInt(),
//            indicatorEndY.toInt()+400
//        )
//        drawable?.draw(canvas!!)
//
//        // 旋转画布
//        canvas?.save()
//        canvas?.rotate(currentRotationAngle, centerX, centerY)

        // 绘制指南针图像等内容

//        canvas?.restore()
    }

}
